package com.canyou.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.canyou.system.entity.dict.ChildDictVO;
import com.canyou.system.entity.dict.DictVO;
import com.canyou.system.entity.dict.ParentDictVO;
import com.canyou.system.mapper.DictMapper;
import com.canyou.system.model.Dict;
import com.canyou.system.service.DictService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author oyj
 * @since 2021年11月16日
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    /**
     * 获取子级分页列表
     *
     * @param page
     * @param name
     * @param parentId
     * @return
     */
    @Override
    public IPage<ChildDictVO> getChildPage(IPage<ChildDictVO> page, String name, Integer parentId) {
        return baseMapper.getChildPage(page, name, parentId);
    }

    /**
     * 获取父级分页
     *
     * @param page
     * @param name
     * @param type
     * @return
     */
    @Override
    public IPage<ParentDictVO> getParentPage(IPage<ParentDictVO> page, String name, String type) {
        return baseMapper.getParentPage(page, name, type);
    }

    /**
     * 获取字典数据数组
     *
     * @param type 字典类型
     * @return
     */
    @Override
    public List<DictVO> getDictList(String type) {
        return baseMapper.getDictList(type);
    }

    /**
     * 根据字典类型和值获取字典名称
     *
     * @param type
     * @param value
     * @return
     */
    @Override
    public String getDictName(String type, Integer value) {
        return baseMapper.getDictName(type, value);
    }

    /**
     * 获取类型值
     *
     * @param type
     * @param name
     * @return
     */
    @Override
    public Integer getDictValue(String type, String name) {
        return baseMapper.getDictValue(type, name);
    }

    /**
     * 根据字段类型获取当前最大值
     *
     * @param type
     * @return
     */
    @Override
    public Integer getDictMaxValue(String type) {
        List<Dict> list = baseMapper.selectList(new QueryWrapper<Dict>().lambda()
                .eq(Dict::getType, type).orderByDesc(Dict::getValue));
        if (list == null || list.size() == 0) {
            return 0;
        }
        Dict dict = list.get(0);
        if (dict.getValue() == null) {
            return 0;
        }
        return dict.getValue();
    }

    /**
     * 验证字典排序值是否重复
     *
     * @param sort
     * @param dictId
     * @return
     */
    @Override
    public boolean validSortRepeat(Integer sort, Long dictId, String type) {
        LambdaQueryWrapper<Dict> queryWrapper = Wrappers.lambdaQuery();
        if (dictId != null && dictId != 0) {
            queryWrapper.ne(Dict::getId, dictId);
        }
        queryWrapper.eq(Dict::getSort, sort);
        queryWrapper.eq(Dict::getType, type);
        return baseMapper.selectList(queryWrapper).isEmpty();
    }

    /**
     * 验证字段值是否重复
     *
     * @param value
     * @param dictId
     * @return
     */
    @Override
    public boolean validDictValue(Integer value, Long dictId, String type) {
        LambdaQueryWrapper<Dict> queryWrapper = Wrappers.lambdaQuery();
        if (dictId != null && dictId != 0) {
            queryWrapper.ne(Dict::getId, dictId);
        }
        queryWrapper.eq(Dict::getValue, value);
        queryWrapper.eq(Dict::getType, type);
        return baseMapper.selectList(queryWrapper).isEmpty();
    }

    /**
     * 验证字典名称是否重复
     *
     * @param id
     * @param name
     * @return
     */
    @Override
    public boolean validDictName(Long id, String name) {
        LambdaQueryWrapper<Dict> queryWrapper = Wrappers.lambdaQuery();
        if (id != null && id != 0) {
            queryWrapper.ne(Dict::getId, id);
        }
        queryWrapper.eq(Dict::getName, name);
        return baseMapper.selectList(queryWrapper).isEmpty();
    }

    /**
     * 验证字典类型是否重复
     *
     * @param id
     * @param type
     * @return
     */
    @Override
    public boolean validDictType(Long id, String type) {
        LambdaQueryWrapper<Dict> queryWrapper = Wrappers.lambdaQuery();
        if (id != null && id != 0) {
            queryWrapper.ne(Dict::getId, id);
        }
        queryWrapper.eq(Dict::getType, type);
        return baseMapper.selectList(queryWrapper).isEmpty();
    }
}
